package com.spzx.report.aiUtils;

import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.service.AiServices;

/*
* 作用: Ai问答
* 模型: 千问/Deepseek
* 平台: 百炼
* 备注: 提供系统提示词模板
* */
public class SpzxAiQuest {
    //千问模型
    private static ChatModel llmQwen;
    //DeepSeek模型
    private static ChatModel llmDeepseek;
    private AssistAgent assistAgent;

    public SpzxAiQuest(){
        //模型初始化
        this.llmQwen = OpenAiChatModel.builder()
                .apiKey(SpzxAiConst.DASHSCOPE_API_KEY)
                .baseUrl(SpzxAiConst.DASHSCOPE_API_BASEURL) // SpzxAiConst.DASHSCOPE_API_BASE_URL;
                .modelName(SpzxAiConst.QWEN_PLUS)// 通义千问
                .build();

        this.llmDeepseek = OpenAiChatModel.builder()
                .apiKey(SpzxAiConst.DASHSCOPE_API_KEY)
                .baseUrl(SpzxAiConst.DASHSCOPE_API_BASEURL) // SpzxAiConst.DASHSCOPE_API_BASE_URL;
                .modelName(SpzxAiConst.DEEPSEEK_V3)// 通义千问
                .build();

        //封装系统提示词
        this.assistAgent = AiServices.builder(AssistAgent.class)
                .chatModel(llmDeepseek)
                .systemMessageProvider((systemPrompt)->PromptGenerator.systemPrompt)
                .build();
    }

    public String processQuestion(String question){
        System.out.println("1 原始问题："+question);

        //组合提示词模板
        System.out.println("2 组合提示词模板=============================================");
        String questionMerge = PromptGenerator.generatePrompt(question);
        System.out.println(questionMerge);

        // Deepseek有效内容裁剪
        System.out.println("3 提问：Deepseek===============================================");
        String answer = assistAgent.chat(questionMerge);
        int jsonStart = answer.lastIndexOf("{");
        int jsonEnd = answer.lastIndexOf("}");


        System.out.println("4 回答：Deepseek===============================================");
        System.out.println(answer);
        String jsonAnswer = answer.substring(jsonStart, jsonEnd+1);
        return  jsonAnswer;
    }
}
